package servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ejb.AccountBeanLocal;
import ejb.LoginBeanLocal;
import entities.Client;

/**
 * Servlet implementation class RegistrationServlet
 */
@WebServlet("/RegistrationServlet")
public class RegistrationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
	
	@EJB
	LoginBeanLocal lb;
	@EJB
	AccountBeanLocal account;
	
    public RegistrationServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Client cl = new Client();
		String e_mail = new String(request.getParameter("login").getBytes("ISO-8859-1"),"UTF-8");
		String e_mail1 = request.getParameter("login");
		String password = new String(request.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
		String password1 = request.getParameter("password");
		String lastName = new String(request.getParameter("lastName").getBytes("ISO-8859-1"),"UTF-8");
		String lastName1 = request.getParameter("lastName");
		String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
		String name1 = request.getParameter("name");
		String secondName = new String(request.getParameter("secondName").getBytes("ISO-8859-1"),"UTF-8");
		String secondName1 = request.getParameter("secondName");
		String gender = new String(request.getParameter("gender").getBytes("ISO-8859-1"),"UTF-8");
		String gender1 = request.getParameter("gender");
		String pasport = new String(request.getParameter("pasport").getBytes("ISO-8859-1"),"UTF-8");
		//String pasport1 = request.getParameter("pasport");
		if (e_mail1=="" | password1=="" | lastName1=="" | name1=="" | secondName1=="" | gender1=="") {
			response.sendRedirect("/NCProjectWeb/Registration.jsp");
		}
		else {
			cl.setName("client" + name);
			cl.setMail(e_mail);
			cl.setPassword(password);
			cl.setSecondname(lastName);
			cl.setFirstname(name);
			cl.setPatronymic(secondName);
			cl.setGender(gender);
			cl.setPassport(pasport);
			cl.setisRegistered("true");
			if (lb.getUser(e_mail)==null) {
				try {
					lb.addUser(cl);
					HttpSession session = request.getSession(true);
					session.setAttribute("Username", e_mail);
					account.initialize(cl);
					response.sendRedirect("/NCProjectWeb/index.jsp");
				}
				catch (Exception e) {e.printStackTrace();}
			}
			else {response.sendRedirect("/NCProjectWeb/Registration.jsp");}
		}
		
	}

}
